package com.liuwei.spring.cloud.framework.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author : DateSerializer
 * @version V1.0
 * @date: 2024/5/13 4:00 PM
 * @Description: TODO
 */
public class DateSerializer extends JsonSerializer<Date> {
    private SimpleDateFormat dateFormat;

    public DateSerializer(String format) {
        super();
        dateFormat = new SimpleDateFormat(format);
    }

    @Override
    public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        String formattedDate = dateFormat.format(value);
        gen.writeString(formattedDate);
    }
}